import qp
from yos.boss import ysearch
from yos.yql import db
import nltk

QTYPE_POINT=100
KEYWORD_COUNT_POINT=8
KEYWORD_ORDER_POING=5
KEYWORD_VARIATION_POINT=2

def search_web(orig_query,ques_point):
	
	count_of_result=10
	rank_list=[]
	solid_ans_max=[-1,"",0,""]
	ans_max=[-1,"",0,""]
	
	data=ysearch.search(orig_query,count=count_of_result,more={'abstract':'long'})
	table=db.create(data=data)
	
	keyword_list=get_keywords(orig_query)
	count=0
	
	for r in table.rows:
		title=nltk.clean_html(r['title'])
		clickurl=nltk.clean_html(r['clickurl'])
		abstract=nltk.clean_html(r['abstract'])
		
		[ans,point]=process_result(keyword_list,ques_point,title,clickurl,abstract)
		rank_list_entry=[count,ans,point,clickurl]
		rank_list.append(rank_list_entry)
		count+=1
		
	#return answer with top points
	#returns only one answer/link...(change to multiple answers/links later)
	for i in rank_list:
		if i[2]>ans_max[2]:
			ans_max=i
			
		if i[1]!=["UNKNOWN_ANSWER"]:
			if i[2]>solid_ans_max[2]:
				solid_ans_max=i
				
	
	if solid_ans_max[0]!=-1:
		return solid_ans_max
	else:
		return ans_max
	

#need to modify in LARGE!!!!!!!!!!!!!!!!!!!!	
def process_result(keyword_list,ques_point,title,clickurl,abstract):
	point=0
	sent_point_list=[]
	abs_sents=nltk.sent_tokenize(abstract)
	for sent in abs_sents:
		sent_toks=nltk.word_tokenize(sent)
		sent_tags=nltk.pos_tag(sent_toks)
		sent_ne=nltk.ne_chunk(sent_tags)
		sent_subtrees=sent_ne.subtrees()
		
		#now check if there are any subtrees, except the first one
		#and see it it has the ANSWER_TYPE
		check=0
		word=[]
		new_point=0
		for i in sent_subtrees:
			if check==0:
				check=1
			else:
				if ques_point==i.node:
					word=get_word_as_list(i.leaves())
					new_point+=QTYPE_POINT
		#now see the no of keywords
		k_point=0
		for k in keyword_list:
			for w in sent_toks:
				if k.lower()== w.lower():
					k_point+=KEYWORD_COUNT_POINT
	
		#to do : keyword_order_point, keyword_variation_point
		if word!=[]:
			sent_point_list.append([word,new_point+k_point])

		point+=k_point
			
	
	#finished main for loop
	#now select the best answer
	max=0
	answer=[["UNKNOWN_ANSWER"],point]

	for ans in sent_point_list:
		if ans[1]> max:
			answer=ans
			max=ans[1]
			
	
	#finished...return answer
	return answer

	
#to be modified to return proper keywords	
def get_keywords(orig_query):
	keyword_tag_list=["CD","JJ","JJR","JJS","LS","NN","NNP","NNPS","NNS"]
	(new_query,modify_list)=qp.modify_query(orig_query)
	toks=nltk.word_tokenize(new_query)
	tags=nltk.pos_tag(toks)
	keyword_list=[]
	for i in tags:
		if i[1] in keyword_tag_list:
			if modify_list.has_key(i[0]):
				keyword_list.append(modify_list[i[0]])
			else:
				keyword_list.append(i[0])
	
	return keyword_list

	
def get_word_as_list(alist):
	wd=[]
	for i in alist:
		wd.append(i[0])
	return wd

	